package madixin.t530;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

class Solution {
    //中序遍历,比较最小的差值
    public int getMinimumDifference(TreeNode root) {
        List<Integer> list = makeTreeToList(root);
        int min = Integer.MAX_VALUE;

        Iterator<Integer> it = list.iterator();
        Integer pre = it.next();
        while (it.hasNext()) {
            Integer next = it.next();
            if (min > next - pre) {
                min = next - pre;
            }
            pre = next;
        }
        return min;
    }

    private List<Integer> makeTreeToList(TreeNode root) {
        List<Integer> list = new LinkedList<>();
        dfs(root, list);
        return list;
    }

    private void dfs(TreeNode root, List<Integer> list) {
        if (root == null) {
            return;
        }
        dfs(root.left, list);
        list.add(root.val);
        dfs(root.right, list);
    }


}